package com.mark.basic.controller;

import com.mark.basic.dto.PhoneDto;
import com.mark.basic.service.IVerifyCodeService;
import com.mark.basic.util.AjaxResult;
import com.mark.basic.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;


    /**
     * 发送手机验证码
     * @param smsDto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody PhoneDto smsDto) {
        try {
            verifyCodeService.sendsmsCode(smsDto);
            return AjaxResult.me();
        } catch (BusinessException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("系统繁忙");
        }
    }


    @GetMapping("/image/{key}")
    public AjaxResult imgVerifyCode(@PathVariable("key") String key ){
        try {
            return AjaxResult.me().setResultObj(verifyCodeService.getImgCode(key));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);
        }

    }
}